package com.example.associationmanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.associationmanager.bean.Funds;
import com.example.associationmanager.bean.Msg;
import com.example.associationmanager.bean.People;
import com.example.associationmanager.bean.User;
import com.example.associationmanager.service.FundsService;
import com.example.associationmanager.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Slf4j
public class UserController {
    @Autowired
    UserService userService;
    @ResponseBody
    @GetMapping("/select/user/all")//获取用户信息
    public Object getFunds(Integer page ,Integer limit,HttpServletRequest request) {
        User userTemp= (User) request.getSession().getAttribute("loginUser");//从session中获取用户的权限
        Integer userAuth=userTemp.getUserAuth()+1;
        Page<User> fundsPage=new Page<>(page, limit);
        Page<User> pageObj = userService.page(fundsPage,new QueryWrapper<User>().eq("user_auth",userAuth));
        List<User> associationAll = pageObj.getRecords();//获取分页查询后的数据
        Msg msg = new Msg(0,"", userService.count(), associationAll);
        return msg;
    }
    @ResponseBody
    @PostMapping("/user/add")//添加用户数据
    public Object  FundsAdd(String userName, String userAccountNumber, String userPassword,@RequestParam(required = false)Integer communityNumber, HttpServletRequest request) {
        User user_account_number = userService.getOne(new QueryWrapper<User>().eq("user_account_number", userAccountNumber));
        if (user_account_number != null) {
            Msg msg = new Msg(200,"failure",null,null);
            return msg;
        }

        User userTemp= (User) request.getSession().getAttribute("loginUser");//从session中获取用户权限
        Integer userAuth=3;
        if(userTemp!=null){
            userAuth=userTemp.getUserAuth();
        }

        if(userAuth.equals(1)){
            userAuth=2;
        }else if (userAuth.equals(2)){
            userAuth = 3;
        }else {//其他情况默认权限均为最低
            userAuth = 3;
        }
        //Integer userAuth=3;//默认最低权限3级
        Integer userStatus=0;//用户状态默认正常
        log.info("/user/add\""+"用户添加成功");
        User user=new User(null,userName,userAccountNumber,userPassword,userAuth,userStatus,communityNumber);
        userService.save(user);//向数据库进行用户对象的插入
        Msg msg = new Msg(200,"success",null,null);
        return msg;

    }
    @ResponseBody
    @GetMapping ("/user/delete")//账号注销
    public Object FundsDelete(@RequestParam("userId") Integer userId) {
        userService.removeById(userId);//删除活动
        log.info("人员注销成功");
        Msg msg = new Msg(200,"success",null,null);
        return msg;
    }
    @ResponseBody
    @PostMapping("/user/update")//更新账号信息
    public Object userUpdate(@RequestParam(required = false)Integer userId,@RequestParam(required = false) String userName,@RequestParam(required = false) String userAccountNumber,@RequestParam(required = false) String userPassword,@RequestParam(required = false) Integer userAuth,@RequestParam(required = false) Integer userStatus,@RequestParam(required = false) Integer communityNumber,HttpServletRequest request) {
        log.info("所属社团"+userAccountNumber);
        //userId一般在session中获取，此处可尝试删除
        User user= (User) request.getSession().getAttribute("loginUser");//从session中获取用户
        userId = user.getUserId();
        User userTwo = new User(userId,userName,userAccountNumber, userPassword, userAuth, userStatus,communityNumber);
        userService.updateById(userTwo);
        Msg msg = new Msg(200,"success",null,null);
        return msg;

    }
}
